package com.ln.adoms.aspect;

import java.util.List;

import com.ln.adoms.dao.MarchandiseDao;
import com.ln.adoms.entity.*;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ln.adoms.dao.GoodsDao;
import com.ln.adoms.dao.OrderLineDao;
import com.ln.adoms.dao.StockDetailDao;
import com.ln.adoms.entity.neum.FullStatus;
import com.ln.adoms.entity.neum.Status;
import com.ln.core.orm.Filtration;
import com.ln.core.orm.Filtration.MatchType;

@Component
@Aspect
public class StockDetailAspect {
    @Autowired
    private StockDetailDao stockDetailDao;

    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private OrderLineDao orderLineDao;
    @Autowired
    private MarchandiseDao marchandiseDao;

    // @Before("execution(* com.ln.adoms.service.*.*(..))")
    // public void doBeforeInServiceLayer() {
    // System.out.println("=====================================");
    // System.out.println("Aop: do before in Service layer");
    // System.out.println("=====================================");
    // }
    //
    // @Around("execution(* com.ln.adoms.service.*.*(..))")
    // public Object doBeforeInServiceLayer(ProceedingJoinPoint pjp) throws Throwable {
    // Object[] ob = pjp.getArgs();
    // for (Object o : ob) {
    // System.out.println(o);
    // }
    //
    // return pjp.proceed();
    // }

    /**
     * 入库的时候，库存统计记录
     *
     * @param jp
     */
    @After("execution(* com.ln.core.orm.hibernate.BaseDao.update(..))")
    public void doBeforeInServiceLayer(JoinPoint jp) {
        try {
            Object[] ob = jp.getArgs();
            if (ob != null && ob.length > 0) {
                // 总代入库
                if (ob[0] instanceof StorageBatch) {
                    StorageBatch batch = (StorageBatch) ob[0];
                    if (FullStatus.ZDYWC.v().equals(batch.getState())) {
                        doBatchRK(batch);
                    }

                } else if (ob[0] instanceof Order) {
                    // 订单入库了
                    Order order = (Order) ob[0];
                    if (order.getStatus().equals(Status.ORDER_HAS_RECEIPT)) {
                        // 进货
                        doOrderJK(order);
                    } else if (order.getStatus().equals(Status.ORDER_HAS_SHIPED)) {
                        // 出货
                        doOrderCK(order);
                    } else if (Status.HAS_BACKED.equals(order.getStatus())) {
                        //退货
                        doOrderTH(order);
                    }
                } else if (ob[0] instanceof Goods) {
                    // 销售
                    Goods goods = (Goods) ob[0];
                    if (goods.getState().equals(Status.HAS_SALED.getValue())) {
                        doGoodsXS(goods);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 新产品入库的处理
     *
     * @param batch
     */
    private void doBatchRK(StorageBatch batch) {

        StockDetail sDetail = new StockDetail();
        sDetail.setAction(ActionStock.RK.getV());
        sDetail.setDepartment(batch.getDepartment());
        sDetail.setTgDepartment(batch.getDepartment());
        sDetail.setMarchandise(batch.getMarchandise());
        Integer stockNumber = 0;
        List<Goods> list = goodsDao.getStockListByMidAndDid(batch.getMarchandise().getId(), batch.getDepartment().getId());
        if (list != null) {
            stockNumber = list.size();
        }
        sDetail.setLeftNumber(stockNumber);
        sDetail.setLeftNumberOfBox(stockNumber / Double.valueOf(batch.getMarchandise().getNumOfBox()));
        sDetail.setNumber(batch.getNumOfMarchandise());
        sDetail.setNumberOfBox(batch.getNumOfBox());
        stockDetailDao.save(sDetail);
    }

    /**
     * 进货入库的处理
     *
     * @param order
     */
    private void doOrderJK(Order order) {
        List<OrderLine> lines = orderLineDao.findList(new Filtration(MatchType.EQ, order.getId(), "order.id"));
        for (OrderLine line : lines) {
            StockDetail sDetail = new StockDetail();
            sDetail.setAction(ActionStock.JH.getV());
            sDetail.setDepartment(order.getDepartment());
            sDetail.setTgDepartment(order.getDepartmentTarget());
            sDetail.setMarchandise(line.getMarchandise());
            Integer stockNumber = 0;
            List<Goods> list = goodsDao.getStockListByMidAndDid(line.getMarchandise().getId(), order.getDepartment().getId());
            if (list != null) {
                stockNumber = list.size() + line.getItems(); // 这个地方要加上这个items的数量，因为它是先更新状态再向goods表中插入数据的
            }
            sDetail.setLeftNumber(stockNumber);
            sDetail.setLeftNumberOfBox(stockNumber / Double.valueOf(line.getMarchandise().getNumOfBox()));
            sDetail.setNumber(line.getItems());
            sDetail.setNumberOfBox(line.getBoxNum());
            stockDetailDao.save(sDetail);
        }
    }

    /**
     * 出库时候的处理
     *
     * @param order
     */
    private void doOrderCK(Order order) {
        List<OrderLine> lines = orderLineDao.findList(new Filtration(MatchType.EQ, order.getId(), "order.id"));
        for (OrderLine line : lines) {
            StockDetail sDetail = new StockDetail();
            sDetail.setAction(ActionStock.CK.getV());
            sDetail.setDepartment(order.getDepartmentTarget());
            sDetail.setTgDepartment(order.getDepartment());
            sDetail.setMarchandise(line.getMarchandise());
            Integer stockNumber = 0;
            List<Goods> list = goodsDao.getStockListByMidAndDid(line.getMarchandise().getId(), order.getDepartmentTarget().getId());
            if (list != null) {
                stockNumber = list.size();
            }
            sDetail.setLeftNumber(stockNumber);
            sDetail.setLeftNumberOfBox(stockNumber / Double.valueOf(line.getMarchandise().getNumOfBox()));
            sDetail.setNumber(line.getItems());
            sDetail.setNumberOfBox(line.getBoxNum());
            stockDetailDao.save(sDetail);
        }
    }

    /**
     * 收到退货的时候处理
     *
     * @param order
     */
    private void doOrderTH(Order order) {

        Department department = order.getDepartment();
        Department dtarget = order.getDepartmentTarget();

        List<OrderLine> lines = orderLineDao.findList(new Filtration(MatchType.EQ, order.getId(), "order.id"));
        for (OrderLine line : lines) {
            Marchandise m = marchandiseDao.find(line.getMarchandise().getId());
            {
                //收到退货的部门
                StockDetail sDetail = new StockDetail();
                sDetail.setAction(ActionStock.BMTH.getV());
                sDetail.setDepartment(dtarget);
                sDetail.setTgDepartment(department);
                sDetail.setMarchandise(line.getMarchandise());
                Integer stockNumber = 0;
                List<Goods> list = goodsDao.getStockListByMidAndDid(line.getMarchandise().getId(), dtarget.getId());
                if (list != null) {
                    stockNumber = list.size();
                }
                sDetail.setLeftNumber(stockNumber);

                sDetail.setLeftNumberOfBox(stockNumber / Double.valueOf(m.getNumOfBox()));
                sDetail.setNumber(line.getItems());
                sDetail.setNumberOfBox(line.getBoxNum());
                stockDetailDao.save(sDetail);
            }
            {
                //退货的部门
                StockDetail sDetail = new StockDetail();
                sDetail.setAction(ActionStock.TH.getV());
                sDetail.setDepartment(department);
                sDetail.setTgDepartment(dtarget);
                sDetail.setMarchandise(line.getMarchandise());
                Integer stockNumber = 0;
                List<Goods> list = goodsDao.getStockListByMidAndDid(line.getMarchandise().getId(), department.getId());
                if (list != null) {
                    stockNumber = list.size();
                }
                sDetail.setLeftNumber(stockNumber);
                sDetail.setLeftNumberOfBox(stockNumber / Double.valueOf(m.getNumOfBox()));
                sDetail.setNumber(line.getItems());
                sDetail.setNumberOfBox(line.getBoxNum());
                stockDetailDao.save(sDetail);
            }
        }
    }

    /**
     * 销售的处理
     *
     * @param goods
     */
    private void doGoodsXS(Goods goods) {

        StockDetail sDetail = new StockDetail();
        sDetail.setAction(ActionStock.XS.getV());
        sDetail.setDepartment(goods.getDepartment());
        sDetail.setTgDepartment(goods.getDepartment());
        sDetail.setMarchandise(goods.getMarchandise());
        Integer stockNumber = 0;
        List<Goods> list = goodsDao.getStockListByMidAndDid(goods.getMarchandise().getId(), goods.getDepartment().getId());
        if (list != null) {
            stockNumber = list.size();
        }
        sDetail.setLeftNumber(stockNumber);
        sDetail.setLeftNumberOfBox(stockNumber / Double.valueOf(goods.getMarchandise().getNumOfBox()));
        sDetail.setNumber(1);
        sDetail.setNumberOfBox(1 / Double.valueOf(goods.getMarchandise().getNumOfBox()));
        stockDetailDao.save(sDetail);
    }
}
